
<?php

function dal_avatar_create($userid, $name, $avatar)
{
	$t_conn   = dal_mocool_get_dbconn();
	$name     = mysql_real_escape_string($name);
	$avatar   = mysql_real_escape_string($avatar);
	$t_sql    = "insert into mocool_avatar_table(user_id, name, avatar) values($userid,'$name','$avatar')";
	$t_result = mysql_query($t_sql, $t_conn);
	if (!$t_result) {
		dal_mysql_error($t_conn, $t_sql, __FUNCTION__);
		return false;
	}
	return true;
}

function dal_avatar_update($userid, $avatar)
{
	$t_conn   = dal_mocool_get_dbconn();
	$avatar   = mysql_real_escape_string($avatar);
	$t_sql    = "update mocool_avatar_table set avatar='$avatar' where user_id=$userid";
	$t_result = mysql_query($t_sql, $t_conn);
	if (!$t_result) {
		dal_mysql_error($t_conn, $t_sql, __FUNCTION__);
		return false;
	}
	return true;
}

function dal_avatar_get($userid)
{
	$t_conn   = dal_mocool_get_dbconn();
	$t_sql    = 'select name, avatar from mocool_avatar_table where user_id=' . $userid;
	$t_result = mysql_query($t_sql, $t_conn);
	if (!$t_result) {
		dal_mysql_error($t_conn, $t_sql, __FUNCTION__);
		return null;
	}
	if (mysql_num_rows($t_result) != 1) {
		return null;
	}
	$row = mysql_fetch_array($t_result);
	return Array(
		'name' => $row['name'],
		'avatar' => $row['avatar']
	);
}

function dal_avatar_exist($userid, $name)
{
	$t_conn = dal_mocool_get_dbconn();
	if ($name == '') {
		$t_sql = "select user_id from mocool_avatar_table where user_id=$userid";
	} else {
		$name  = mysql_real_escape_string($name);
		$t_sql = "select user_id from mocool_avatar_table where user_id=$userid or name='$name'";
	}
	$t_result = mysql_query($t_sql, $t_conn);
	if (!$t_result) {
		dal_mysql_error($t_conn, $t_sql, __FUNCTION__);
		return false;
	}
	return mysql_num_rows($t_result) > 0;
}

function dal_avatar_infos($uids)
{
	$t_first = true;
	$t_sql   = 'select user_id, name from mocool_avatar_table where user_id IN (';
	foreach ($uids as $key => $val) {
		if ($t_first) {
			$t_first = false;
			$t_sql   = $t_sql . $val;
		} else {
			$t_sql = $t_sql . ',' . $val;
		}
	}
	if ($t_first)
		return Array();
	$t_sql = $t_sql . ')';
	
	$t_conn   = dal_mocool_get_dbconn();
	$t_result = mysql_query($t_sql, $t_conn);
	if (!$t_result) {
		dal_mysql_error($t_conn, $t_sql, __FUNCTION__);
		return null;
	}
	
	$t_ret = Array();
	while ($row = mysql_fetch_array($t_result)) {
		$t_ret[$row['user_id']] = $row['name'];
	}
	
	return $t_ret;
}

?>
